package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.entity.KeyValueForDate;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import com.itheima.utils.SplitDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.*;


/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

//    根据时间段查询会员数量
    @Override
    public Map<String, Object> getMemberByTimeQuantum(String beginDate, String endDate ) {
//        将最终待返回的数据封装为map
        Map<String, Object> date = new HashMap<>();
//        将待返回的月数集合封装为string list
        List<String> months = new ArrayList();
//        将待返回的月会员数的集合封装为int list
        ArrayList<Integer> memberCountList = new ArrayList<>();
//        调用工具类，将时间段按月划分
        List<KeyValueForDate> keyValueForDate = SplitDateUtils.getKeyValueForDate(beginDate, endDate);
        for (int i = 0; i < keyValueForDate.size(); i++) {
//            将月数添加进months
            String[] strings = keyValueForDate.get(i).getStartDate().split("-");
            months.add(strings[0] + "-" + strings[1]);
//            调用dao查询每个月区间段的会员数
            Integer count = memberDao.findMemberCountByTimeQuantum(keyValueForDate.get(i).getStartDate(), keyValueForDate.get(i).getEndDate());
//            将月区间的会员数添加进memberCountList
            memberCountList.add(count);
        }
        date.put("months", months);
        date.put("memberCount", memberCountList);
        return date;
    }

    @Override
    public Map<String,Integer> getMemberSexAnalysis(){
        Map<String, Integer> map = new HashMap<>();
        int manNumber = memberDao.findMan();
        int womenNumber = memberDao.findWomen();
        map.put("manNumber", manNumber);
        map.put("womenNumber", womenNumber);
       return map;
    }

    //获取年龄段信息

    public Map<String, Integer> getMemberAgeAnalysis ()throws Exception {
        Map<String, Integer> map = new HashMap<>();
        map.put("age1", memberDao.findMemberCountByAgeScope(getAgeScope(0, 18)));
        map.put("age2", memberDao.findMemberCountByAgeScope(getAgeScope(18, 30)));
        map.put("age3", memberDao.findMemberCountByAgeScope(getAgeScope(30, 45)));
        map.put("age4", memberDao.findMemberCountByAgeScope(getAgeScope(45, null)));
        map.put("age5",memberDao.findMemberWithoutBirth());
        return map;
    }

    private Map<String, String> getAgeScope(Integer start, Integer end) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -start);
        String endTime = DateUtils.parseDate2String(calendar.getTime());
        map.put("end", endTime);
        if (end == null) map.put("start",null);
        else{
            calendar.add(Calendar.YEAR, start - end);
            String startTime = DateUtils.parseDate2String(calendar.getTime());
            map.put("start", startTime);
        }



        return map;
    }
}
